package com.qf.gh.web.servlet;

import com.qf.gh.domain.Cart;
import com.qf.gh.domain.Goods;
import com.qf.gh.domain.User;
import com.qf.gh.service.CartService;
import com.qf.gh.service.GoodsService;
import com.qf.gh.service.impl.CartServiceImpl;
import com.qf.gh.service.impl.GoodaServiceImpl;
import com.qf.gh.utils.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet {
    CartService cartService = new CartServiceImpl();

    public void addCartAjax(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        String number = request.getParameter("number");
        String goodsId = request.getParameter("goodsId");
        GoodsService goodsService=new GoodaServiceImpl();
        Cart cart = cartService.findByUidAndGid(user.getId(),Integer.parseInt(goodsId));
        Goods goods = goodsService.findById(Integer.parseInt(goodsId));
        if(Integer.parseInt(number)==0){
            cartService.delect(user.getId(),Integer.parseInt(goodsId));
        }else{
            cart.setNum(cart.getNum()+Integer.parseInt(number));
            cart.setMoney(goods.getPrice() * cart.getNum());
            cartService.update(cart);
        }

    }

    public void clearCartAjax(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        cartService.clearByUid(user.getId());


    }

    public String getCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        List<Cart> carts=cartService.findByUid(user.getId());
        request.setAttribute("carts",carts);
        return "forward:/cart.jsp";
    }
    public String addCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if (StrUtils.isEmpty(goodsId)||StrUtils.isEmpty(number)) {
            request.setAttribute("msg", "添加购物车失败");
            return "forward:/message.jsp";
        }
        GoodsService goodsService = new GoodaServiceImpl();
        Goods goods = goodsService.findById(Integer.parseInt(goodsId));
        Cart cart = cartService.findByUidAndGid(user.getId(), Integer.parseInt(goodsId));
        if (cart == null) {
            cart = new Cart();
            cart.setId(user.getId());
            cart.setPid(Integer.parseInt(goodsId));
            cart.setMoney(Integer.parseInt(number) * goods.getPrice());
            cart.setNum(Integer.parseInt(number));
            cartService.insert(cart);
        } else {
            cart.setNum(cart.getNum()+Integer.parseInt(number));
            System.out.println(cart.getNum());
            cart.setMoney(goods.getPrice() * cart.getNum());
            cartService.update(cart);
        }
        return "redirect:/cartSuccess.jsp";
    }
}